package net.dgg.rz.production.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.rz.common.constant.ModuleCodeConstant;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.utils.DataPermissionUtils;
import net.dgg.rz.common.utils.DataTableResponse;
import net.dgg.rz.common.utils.ExcelTool;
import net.dgg.rz.common.utils.StringUtils;
import net.dgg.rz.production.dao.PunishSchemeDao;
import net.dgg.rz.production.entity.NodeTimeOutPunish;
import net.dgg.rz.production.entity.PunishScheme;
import net.dgg.rz.production.entity.dto.ProductOrderDto;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.rz.production.service.PunishSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程节点超级扣罚方案controller
 * Created by cuipeng on 2019/03/19.
 */
@Controller
@RequestMapping("/rzsc/punish")
public class PunishSchemeController extends DggBaseController{
    @Autowired
    private PunishSchemeService punishSchemeService;

    @Autowired
    private PunishSchemeDao punishSchemeDao;
    @Autowired
    private DataPermissionUtils dataPermissionUtils;

    /**
     * 扣罚方案列表页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(Model model) {
        model.addAttribute("ytCode",ScProductConstant.BUS_YT_DK);
        model.addAttribute("ytName",ScProductConstant.BUS_YT_DK_NAME);
        return "rzsc/punish/punish_index";
    }

    /**
     * @Title: 查询扣罚方案数据
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "/search_all_punish")
    @ResponseBody
    public DggRestResponse searchAllPunish(@RequestParam Map params){
        try {
            List<PunishScheme> list = punishSchemeService.queryPunishList(params);
            return new DataTableResponse().data(params,list);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Title: 查询扣罚详情
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "/search_all_punish_detail")
    @ResponseBody
    public DggRestResponse searchAllPunishDetail(@RequestParam Map params){
        try {
            List<Map> list = punishSchemeService.searchAllPunishDetail(params);
            return new DataTableResponse().data(params,list);
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Title: 新增方案页面跳转
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "/add_punish_tabpage")
    public String addPunishTabpage(Model model) {
        model.addAttribute("ytCode",ScProductConstant.BUS_YT_DK);
        model.addAttribute("ytName",ScProductConstant.BUS_YT_DK_NAME);
        return "rzsc/punish/add_punish";
    }
    /**
     * @Title: 新增方案页面跳转
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "/add_punish_infor_tabpage")
    public String addPunishInforTabpage(Model model,Long punishId) {
        model.addAttribute("ytCode",ScProductConstant.BUS_YT_DK);
        model.addAttribute("ytName",ScProductConstant.BUS_YT_DK_NAME);
        model.addAttribute("punishId",punishId);
        return "rzsc/punish/add_punishinfor";
    }
    /**
     * @Title: 编辑方案页面跳转
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "/edit_punish_tabpage")
    public String editPunishTabpage(Model model,Long id,String type) {
        punishSchemeService.editPunish(model,id);
        model.addAttribute("type",type);
        model.addAttribute("punishId",id);
        return "rzsc/punish/edit_punish";
    }

    /**
     * @Title: 新增方案
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "add_punish")
    @ResponseBody
    public DggRestResponse addPunish(@RequestParam Map params){
        try {
            punishSchemeService.addPunish(params);
            return this.getSuccessResponse("保存成功");
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Title: 修改方案
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "update_punish")
    @ResponseBody
    public DggRestResponse updatePunish(@RequestParam Map params){
        try {
            punishSchemeService.UpdatePunish(params);
            return this.getSuccessResponse("修改成功");
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * @Title: 修改扣罚方明细列表
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "edit_punish_detail_list")
    @ResponseBody
    public DggRestResponse editPunishDetailList(@RequestParam Map params){
        try {
            punishSchemeService.editPunisDetailList(params);
            return this.getSuccessResponse("修改成功");
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * @Title: 新增扣罚详情
     * @author: cuipeng
     * @creat_date: 2019/03/19
     **/
    @RequestMapping(value = "add_punish_infor")
    @ResponseBody
    public DggRestResponse addPunishInfor(@RequestParam Map params){
        try {
            punishSchemeService.addPunishInfor(params);
            return this.getSuccessResponse("操作成功");
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }


    /**
     * 获取扣罚方案列表数据
     *
     * @return
     */
    @RequestMapping(value = "/query_punish_json")
    @ResponseBody
    public DggRestResponse queryPinshiJson(String code){
        try {
            PunishScheme p = new PunishScheme();
            p.setBusinessTypeCode(code);
            return this.getSuccessResponse(punishSchemeDao.queryList(p));
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 获取团队
     *
     * @return
     */
    @RequestMapping(value = "/query_team")
    @ResponseBody
    public DggRestResponse queryTeam(String code){
        try {
            List<Map<String,Object>> list = new ArrayList<>();
            list.add(new HashMap(){{put("id","1");put("name","张三团队");}});
            list.add(new HashMap(){{put("id","2");put("name","李四团队");}});
            list.add(new HashMap(){{put("id","3");put("name","王五团队");}});
            list.add(new HashMap(){{put("id","4");put("name","陈六团队");}});
            list.add(new HashMap(){{put("id","5");put("name","田七团队");}});
            list.add(new HashMap(){{put("id","6");put("name","万八团队");}});
            list.add(new HashMap(){{put("id","7");put("name","官九团队");}});
            return this.getSuccessResponse(list);
        }catch (Exception e){
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 修改单条扣罚值
     */
    @RequestMapping("/edit_punish_infor")
    @ResponseBody
    public DggRestResponse editPunishInfor(@RequestParam Map params){
        try {
            punishSchemeService.editPunishInfor(params);
            return this.getSuccessResponse("修改成功");
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 查询所有节点超时扣罚列表，页面跳转
     */
    @RequestMapping(value = "/node_timeout_punish_index")
    public String nodeTimeoutPunishTabpage(Model model) {
        return "rzsc/punish/node_timeout_pinish_list";
    }
    /**
     * 查询所有节点超时扣罚列表数据
     */
    @RequestMapping("/node_timeout_punish")
    @ResponseBody
    public DggRestResponse nodeTimeoutPunish(@RequestParam Map params){
        try {
            return new DataTableResponse().data(params,punishSchemeService.nodeTimeoutPunish(params));
        }catch (ProductOrderExcption e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(ScProductConstant.SYS_ERROR);
        }
    }

    /**
     * 导出
     */
    @RequestMapping("/export_node_timeout_punish")
    @ResponseBody
    @SuppressWarnings("all")
    public void exportNodeTimeoutPunish(@RequestParam Map params){
        int[] widths = new int[] { 512*8,512*8,512*8, 512*8, 512*8, 512*8, 512*8, 512*8};
        int[] styleArr = new int[] { -1,-1,-1, 0, 0, 0 , 0, 0};
        if(StringUtils.isNotEmpty(params.get("timeOut"))){
            String timeOut=String.valueOf(params.get("timeOut"));
            String timeOutStart=timeOut.substring(0,timeOut.indexOf("~"));
            timeOutStart= timeOutStart.trim();
            String timeOutEnd = timeOut.substring(timeOut.indexOf("~") + 1);
            timeOutEnd= timeOutEnd.trim();
            params.put("timeOutStart", timeOutStart);
            params.put("timeOutEnd", timeOutEnd);
        }
        List<NodeTimeOutPunish> list =punishSchemeService.nodeTimeoutPunish(params);
        String sheetName  = "节点超期扣罚";
        String[] title = null;
        String[] keyArray = null;
        title = new String[]{"流程人员","生产事业部","销售订单编号","生产单编号","超期节点","超期日期","扣罚操行分","扣罚金额"};
        keyArray = new String[] {"flowUserName","productOrgName","orderNo","scProductOrderNo","noName","timeOutTime","points","money"};
        ExcelTool.execlExport(list,sheetName,title,keyArray,widths,styleArr);
    }

}
